// JavaScript Document
var map;
var currentCity;
var point;
$(function() {
	init();
});

//初始化地图
function init() {
	map = new BMap.Map("allmap");
	map.centerAndZoom("北京市");
	map.enableScrollWheelZoom();
	map.enableInertialDragging();
	map.enableContinuousZoom();
	map.disableDoubleClickZoom();
	addMenu();
	addSearchInput();
	
	//添加右击监听
	map.addEventListener("rightclick",function(e){
		point = new BMap.Point(e.point.lng,e.point.lat);
	});
	
}

//根据ip定位当前城市
function getCurrentCity() {
	function myFun(result){
		currentCity = result.name;
	}
	var myCity = new BMap.LocalCity();
	myCity.get(myFun);
}

//根据浏览器定位当前经纬度
function getCurrentPoint() {
	var point = (0, 0);
	var geolocation = new BMap.Geolocation();
	geolocation.getCurrentPosition(function(r){
		if(this.getStatus() == BMAP_STATUS_SUCCESS){
			var mk = new BMap.Marker(r.point);
			map.addOverlay(mk);
			map.panTo(r.point);
			point = new BMap.Point(r.point.lng,r.point.lat);
		} else {
			alert(this.getStatus());
		}        
	},{enableHighAccuracy: true})
}

//添加右键菜单
function addMenu() {
	var menu = new BMap.ContextMenu();
	var txtMenuItem = [
		{
			text:'添加设备位置',
			callback:function(){
				$("#add_shebei_info").dialog("open");
				$("#add_lng").val(getPoint().lng);
				$("#add_lat").val(getPoint().lat);
			}
		}
	];
	for(var i=0; i < txtMenuItem.length; i++){
		menu.addItem(new BMap.MenuItem(txtMenuItem[i].text,txtMenuItem[i].callback,100));
	}
	map.addContextMenu(menu);
}

//添加搜索框插件
function addSearchInput(){
	function ZoomControl() {
		// 设置默认停靠位置和偏移量  
	    this.defaultAnchor = BMAP_ANCHOR_TOP_LEFT;    
	    this.defaultOffset = new BMap.Size(20, 10);    
	}
	
	ZoomControl.prototype = new BMap.Control();
	
	//自定义控件必须实现initialize方法，并且将控件的DOM元素返回   
	//在本方法中创建个div元素作为控件的容器，并将其添加到地图容器中   
	ZoomControl.prototype.initialize = function(map){    
		//创建一个DOM元素   
		var div = document.createElement("div");    
		
		//设置样式
		div.style.width = "270px";    
		div.style.height = "30px";
		div.style.background = "white";
		div.style.lineHeight = "30px";
		div.style.paddingLeft = "5px";
		div.style.fontSize = "14px";
		div.style.fontWeight = "bold";
		div.appendChild(document.createTextNode("请输入："));
		
		var input = document.createElement("input");
		input.style.padding="2px";
		input.style.width = "200px";    
		input.style.height = "20px";
		input.id="suggestId";
		div.appendChild(input);
		
		// 添加DOM元素到地图中   
		map.getContainer().appendChild(div);
		// 将DOM元素返回  
		return div;
	}

    // 创建控件实例    
    var myZoomCtrl = new ZoomControl();    
    // 添加到地图当中    
    map.addControl(myZoomCtrl);
    
    searchInput();
}

//搜索的方法
function searchInput() {
	function G(id) {
		return document.getElementById(id);
	}

	var ac = new BMap.Autocomplete(    //建立一个自动完成的对象
		{"input" : "suggestId"
		,"location" : map
	});

	ac.addEventListener("onhighlight", function(e) {  //鼠标放在下拉列表上的事件
		var str = "";
		var _value = e.fromitem.value;
		var value = "";
		if (e.fromitem.index > -1) {
			value = _value.province +  _value.city +  _value.district +  _value.street +  _value.business;
		}    
		str = "FromItem<br />index = " + e.fromitem.index + "<br />value = " + value;
		
		value = "";
		if (e.toitem.index > -1) {
			_value = e.toitem.value;
			value = _value.province +  _value.city +  _value.district +  _value.street +  _value.business;
		}    
		str += "<br />ToItem<br />index = " + e.toitem.index + "<br />value = " + value;
		G("searchResultPanel").innerHTML = str;
	});

	var myValue;
	ac.addEventListener("onconfirm", function(e) {    //鼠标点击下拉列表后的事件
		var _value = e.item.value;
		myValue = _value.province +  _value.city +  _value.district +  _value.street +  _value.business;
		G("searchResultPanel").innerHTML ="onconfirm<br />index = " + e.item.index + "<br />myValue = " + myValue;
		
		setPlace();
	});
	
	function setPlace(){
		map.clearOverlays();    //清除地图上所有覆盖物
		function myFun(){
			var pp = local.getResults().getPoi(0).point;    //获取第一个智能搜索的结果
			map.centerAndZoom(pp, 18);
			map.addOverlay(new BMap.Marker(pp));    //添加标注
		}
		var local = new BMap.LocalSearch(map, { //智能搜索
		  onSearchComplete: myFun
		});
		local.search(myValue);
	}
}

//返回右击的坐标
function getPoint() {
	return point;
}

//显示设备位置
function showInMap(value) {
	
}

